package com.wuhongyu.service.impl;

import com.wuhongyu.base.BaseServiceImpl;
import com.wuhongyu.common.MyCommon;
import com.wuhongyu.entity.Refer;
import com.wuhongyu.entity.User;
import com.wuhongyu.repository.ReferDao;
import com.wuhongyu.repository.UserDao;
import com.wuhongyu.base.BaseService;
import com.wuhongyu.service.ReferService;
import com.wuhongyu.util.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("referService")
public class ReferServiceImpl extends BaseServiceImpl<Refer> implements ReferService {

    @Autowired
    private UserDao userDao;

    private ReferDao referDao;

    @Resource
    public void setDao(ReferDao referDao){
        super.setBaseDao(referDao);
        this.referDao = referDao;
    }


    /**
     * 查询还未分配的人
     * @param refer
     * @param number
     * @return
     */
    @Override
    public Page<Refer> findAll(Refer refer, Integer number) {
        Specification<Refer> spec = (root, query, cb) -> {
            List<Predicate> list = new ArrayList<>();
            if (MyUtil.notEmpty(refer.getName()))
                list.add(cb.like(root.get("name"), "%" + refer.getName() + "%"));
            if (MyUtil.notEmpty(refer.getOutline()))
                list.add(cb.like(root.get("outline"), "%" + refer.getOutline() + "%"));
            if (MyUtil.notEmpty(refer.getContacts()))
                list.add(cb.like(root.get("contacts"), "%" + refer.getContacts() + "%"));
            list.add(cb.equal(root.get("status"), 0));
            return cb.and(list.toArray(new Predicate[list.size()]));
        };
        return referDao.findAll(spec, new PageRequest(MyUtil.getCurrentPage(number), MyCommon.PAGESIZE));
    }

    /**
     * 销售机会管理的新建和修改
     * @param refer
     */
    @Override
    public void save(Refer refer) {
        //如果没有分配的人,就把这个对象置为空
        if (refer.getAppointUser().getId() == null) {
            refer.setAppointUser(null);
            //状态0为未分配状态
            refer.setStatus(0);
            //如果有分配的人
        }else {
            //将分配人的信息放进来
            User user = userDao.findById(refer.getAppointUser().getId()).get();
            refer.setAppointUser(user);
            //如果没有分配时间或者分配人改变了,就将时间加上
            if (refer.getAppointtime() == null || (refer.getAppointUser().getId() != user.getId()))
                refer.setAppointtime(new Date());
            //状态1为已分配状态
            refer.setStatus(1);
        }
        //一定会有创建人
        //if(refer.getCreateuser().getId()!=null)
        //将创建人的信息放进来
        refer.setCreateuser(userDao.findById(refer.getCreateuser().getId()).get());
        //执行保存操作
        referDao.saveAndFlush(refer);
    }

    /**
     * 查找已经分配了的人
     * @param refer
     * @param number
     * @return
     */
    @Override
    public Page<Refer> findRefer(Refer refer, Integer number){
        Specification<Refer> spec = (root, query, cb) -> {
            List<Predicate> list = new ArrayList<>();
            if (MyUtil.notEmpty(refer.getName()))
                list.add(cb.like(root.get("name"), "%" + refer.getName() + "%"));
            if (MyUtil.notEmpty(refer.getOutline()))
                list.add(cb.like(root.get("outline"), "%" + refer.getOutline() + "%"));
            if (MyUtil.notEmpty(refer.getContacts()))
                list.add(cb.like(root.get("contacts"), "%" + refer.getContacts() + "%"));

            //已分配的人都是比0大的状态
            list.add(cb.gt(root.get("status"), 0));
            return cb.and(list.toArray(new Predicate[list.size()]));
        };
        return referDao.findAll(spec, new PageRequest(MyUtil.getCurrentPage(number), MyCommon.PAGESIZE));
    }

    /**
     * 分配
     * @param refer
     */
    @Override
    public void dispatch(Refer refer){
        //一定会有id
        Refer refer1 = referDao.findById(refer.getId()).get();
        //一定会有分配人,要不然你点进来干嘛...
        //将分配人的信息放进来
        User user = userDao.findById(refer.getAppointUser().getId()).get();
        refer1.setAppointUser(user);
        //状态1为已分配状态
        refer1.setStatus(1);
        //也一定会有分配时间
        refer1.setAppointtime(refer.getAppointtime());
        referDao.saveAndFlush(refer1);
    }

    /**
     * 修改开发成功或者失败时的状态
     * @param referid
     * @param status
     */
    @Override
    public void change(Integer referid,Integer status){
        Refer refer = referDao.findById(referid).get();
        refer.setStatus(status);
        referDao.saveAndFlush(refer);
    }


}